package demointerface;

class Money implements Cloneable {
    public double money = 12.5;

    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
class Person implements Cloneable {
    public String name;
    public Money m;
    public Person(String name) {
        this.name = name;
        m = new Money();
    }
    //重写clone方法
    @Override
    protected Object clone() throws CloneNotSupportedException {
        //return super.clone();
        //深拷贝
        Person tmp = (Person) super.clone();
        tmp.m = (Money) this.m.clone();
        return tmp;
    }
}
public class Test2 {
    public  static void main(String[] args) throws CloneNotSupportedException {
        //深浅拷贝
        //深拷贝:完全克隆出完全独立于原本对象的对象
        Person person1 = new Person("张三");
        Person person2 = (Person)person1.clone();
        System.out.println("person1拷贝前: " + person1.m.money);
        System.out.println("person2拷贝前: " + person2.m.money);
        person2.m.money = 99.99;
        System.out.println("person1拷贝后: " + person1.m.money);
        System.out.println("person2拷贝后: " + person2.m.money);
    }
    public static void main1(String[] args) throws CloneNotSupportedException {//main方法也需要抛异常
        //clone接口
        Person person1 = new Person("zhangsan");
        Person person2  = (Person)person1.clone();
        System.out.println(person2.name);
    }
}
